﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Seller
{
    public partial class mftemp2 :msBsc.Cls.mbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 根据IP获取省市
        /// </summary>
        public void GetAddressByIp()
        {

            //string ip = "115.193.217.249";
            string ip = this.GetParamHtValue("ip");
            string PostUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
            string res = GetDataByPost(PostUrl);//该条请求返回的数据为：res=1\t115.193.210.0\t115.194.201.255\t中国\t浙江\t杭州\t电信
            this.ExecJScript(@"GetIps(" + res + ")");
            //Alert(HttpUtility.UrlDecode(res));
            //string[] arr = getAreaInfoList(res);
            
            
        }

        /// <summary>
        /// Post请求数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetDataByPost(string url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            string s = "anything";
            byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = requestBytes.Length;
            
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
            string backstr = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return backstr;
        }

        /// <summary>
        /// 处理所要的数据
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static string[] getAreaInfoList(string ipData)
        {
            //1\t115.193.210.0\t115.194.201.255\t中国\t浙江\t杭州\t电信
            string[] areaArr = new string[10];
            string[] newAreaArr = new string[2];
            try
            {
                //取所要的数据，这里只取省市
                areaArr = ipData.Split('\t');
                newAreaArr[0] = areaArr[4];//省
                newAreaArr[1] = areaArr[5];//市
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
            return newAreaArr;
        }

        public override void DoAction()
        {
            if (this.strAction=="init")
            {
                //GetAddressByIp();
                //msBsc.SvRef.IpSearch ipSearch = new SvRef.IpSearch();
                string ip = this.GetParamHtValue("ip");
                string res =msBsc.SvRef.IpSearch.GetAddressTaobaoIp(ip);
                this.ExecJScript(@"GetIps(" + res + ")");
                
                GetJsonValue(res, "code");
            }
        }

        private string GetJsonValue(string json, string key)
        {
            try
            {
                JArray jsonObj = (JArray)JsonConvert.DeserializeObject(json);
                return jsonObj[0][key].ToString();
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}